package com.woniuxy.kaer.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.OSSUtil;
import com.woniuxy.kaer.param.AddPointProductParam;
import com.woniuxy.kaer.shop.model.PointProduct;
import com.woniuxy.kaer.shop.mapper.PointProductMapper;
import com.woniuxy.kaer.shop.model.Product;
import com.woniuxy.kaer.shop.service.PointProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭建东
 * @since 2025年02月17日
 */
@Service
public class PointProductServiceImpl extends ServiceImpl<PointProductMapper, PointProduct> implements PointProductService {

    @Resource
    private OSSUtil ossUtil;

    @Resource
    private PointProductMapper pointProductMapper;

    @Override
    public void addPointProduct(AddPointProductParam param)throws Exception {
        //判断上传的文件的真实类型
        //取出上传的文件的前4个字节，其中保存了文件的类型信息
        InputStream is = param.getPointProductPicture().getInputStream();
        byte[] fileHeader=new byte[4];
        is.read(fileHeader);
        //调用API判断文件类型
        if(!(ossUtil.isJpg(fileHeader)||ossUtil.isPng(fileHeader))){
            throw new Exception("文件类型不支持");
        }
        if(param==null){
            throw new Exception("商品数据为空");
        }
        if(param.getPointNeed() <= 0){
            throw new Exception("商品积分必须大于0");
        }
        QueryWrapper<PointProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("point_product_name",param.getPointProductName());
        //判断商品是否存在(名字）
        if (pointProductMapper.selectOne(queryWrapper)!=null){
            throw new Exception("该商品已存在");
        }
        //先获取文件的后缀名
        String suffix = StringUtils.getFilenameExtension(param.getPointProductPicture().getOriginalFilename());
        String prefix= UUID.randomUUID().toString();
        //实现文件上传
        String url = ossUtil.uploadFile(param.getPointProductPicture().getInputStream(), prefix + "." + suffix, "guojiandong");
        //使用工具类将数据从param复制到product
        PointProduct product = BeanUtil.toBean(param, PointProduct.class);
        product.setPointProductPicture(url);
        //库存为0,状态0默认为下架,冻结库存为0
        product.setPointProductStock(0L);
        product.setPointProductFreeze(0L);
        product.setPointProductState(0);
        //创建时间
        product.setCreateTime(LocalDateTime.now().toString());
        pointProductMapper.insert(product);
    }

    @Override
    public void updateInventory(Integer id, Long inventory) throws Exception {
        QueryWrapper<PointProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        //判断商品是否存在
        PointProduct pointProduct = pointProductMapper.selectOne(queryWrapper);
        if(pointProduct==null){
            throw new Exception("商品不存在");
        }
        if(inventory+pointProduct.getPointProductStock()<=0){
            throw new Exception("商品库存必须大于等于0");
        }
        pointProduct.setPointProductStock(inventory+pointProduct.getPointProductStock());
        pointProductMapper.updateById(pointProduct);
    }
}
